from time import timezone

from django.shortcuts import render
from flight.models import *
from userInformation.models import *
from userInformation.views import *
import json
from django.shortcuts import HttpResponse
from datetime import datetime, timedelta





def DateCompare(dateA, dateB, days, minutes, hours, weeks):
    time = timedelta(days=days, minutes=minutes, hours=hours, weeks=weeks)

    if dateB.__ge__(dateA + time):
        return True
    else:
        return False


def searchFlight(request):
    if request.method == 'POST':
        req = json.loads(request.body)
        startpos = req['startPos']
        endpos = req['endPos']
        flag = 0
        startTime = req['startTime']
        aimflights = FlightInfo.objects.filter(startPos=startpos, endPos=endpos)
        data = {'root': []}
        i = 0
        for flight in aimflights:
            if flight.dboardingTime.__ge__(timezone.now()):
                if (str(startTime) != str(flight.startTime).split(' ')[0]):
                    continue
                info = {}
                temp = []
                no = 'flightName'
                info[no] = flight.flightName
                starttime = 'startTime'
                info[starttime] = str(flight.startTime)
                endtime = 'endTime'
                info[endtime] = str(flight.endTime)
                info['startPos'] = flight.startPos
                info['endPos'] = flight.endPos
                info['flightId'] = flight.id
                info['startAir'] = flight.startAir
                info['endAir'] = flight.endAir
                info['price'] = str(flight.price)
                info['leftSeat'] = flight.leftSeat
                info['state'] = flight.state
                i = i + 1
                temp.append(info)
                flag = 1
                data['root'].append(temp)
        if aimflights:
            if flag:
                return HttpResponse(json.dumps(data))
            else:
                return mutilFlight(request)
    else:
        data = {
            'success': 0,
        }
        return HttpResponse(json.dumps(data))


class items:

    def __init__(self, start, end, price, flightName, last, startTime, endTime, startAir, endAir, leftSeat, state, id):
        self.start = start
        self.end = end
        self.price = price
        self.count = 0
        self.flightName = flightName
        self.last = last
        self.startTime = startTime
        self.endTime = endTime
        self.startAir = startAir
        self.endAir = endAir
        self.leftSeat = leftSeat
        self.state = state
        self.id = id


def mutilFlight(request):
    if request.method == 'POST':
        req = json.loads(request.body)
        start = req['startPos']
        end = req['endPos']
        aimTime = req['startTime']
        firsts = []
        seconds = []
        thirds = []
        aimflights1 = FlightInfo.objects.filter(startPos=start)
        for flight in aimflights1:
            if flight.leftSeat > 0 and (
                    str(aimTime) == str(flight.startTime).split(' ')[0]) and flight.dboardingTime.__ge__(
                timezone.now()):
                firsts.append(items(flight.startPos, flight.endPos, flight.price, flight.flightName, None,
                                    flight.startTime,
                                    flight.endTime, flight.startAir, flight.endAir, flight.leftSeat, flight.state,
                                    flight.id))
        for first in firsts:
            aimflights2 = FlightInfo.objects.filter(startPos=first.end)
            for aimflight2 in aimflights2:
                if ((aimflight2.startAir != first.endAir and DateCompare(first.endTime, aimflight2.startTime, 0, 0, 5,
                                                                         0)) or (
                            aimflight2.startAir == first.endAir and DateCompare(first.endTime, aimflight2.startTime, 0,
                                                                                0, 1,
                                                                                0))) and aimflight2.leftSeat and not DateCompare(
                    first.endTime, aimflight2.startTime, 0,
                    0,
                    10,
                    0):
                    seconds.append(
                        items(aimflight2.startPos, aimflight2.endPos, aimflight2.price + first.price,
                              aimflight2.flightName,
                              first, aimflight2.startTime, aimflight2.endTime, aimflight2.startAir, aimflight2.endAir,
                              flight.leftSeat, flight.state, flight.id))

        for second in seconds:
            if not second.end == end:
                aimflights3 = FlightInfo.objects.filter(startPos=second.end)
                for aimflight3 in aimflights3:

                    if ((aimflight3.startAir != second.endAir
                         and DateCompare(second.endTime, aimflight3.startTime, 0,
                                         0,
                                         5,
                                         0)) or (
                                aimflight3.startAir == second.endAir and DateCompare(second.endTime,
                                                                                     aimflight3.startTime,
                                                                                     0,
                                                                                     0, 1,
                                                                                     0))) and aimflight3.leftSeat and not DateCompare(
                        second.endTime, aimflight3.startTime, 0,
                        0,
                        10,
                        0):
                        thirds.append(
                            items(aimflight3.startPos, aimflight3.endPos, aimflight3.price + second.price,
                                  aimflight3.flightName,
                                  second, aimflight3.startTime, aimflight3.endTime, aimflight3.startAir,
                                  aimflight3.endAir, flight.leftSeat, flight.state, flight.id))
        count = 0
        data = {'root': []}
        for second in seconds:
            if second.end == end:
                no = []
                transferInfo = {}
                transferInfo['order'] = 2
                transferInfo['start'] = second.start
                transferInfo['end'] = second.end
                transferInfo['startAir'] = second.startAir
                transferInfo['endAir'] = second.endAir
                transferInfo['startTime'] = str(second.startTime)
                transferInfo['endTime'] = str(second.endTime)
                transferInfo['flightName'] = second.flightName
                transferInfo['price'] = str(second.price)
                transferInfo['leftSeat'] = second.leftSeat
                transferInfo['state'] = second.state
                transferInfo['flightId'] = second.id
                no.append(transferInfo)
                prev = second.last
                transferInfo2 = {}
                transferInfo2['start'] = prev.start
                transferInfo2['end'] = prev.end
                transferInfo2['order'] = 1
                transferInfo2['startAir'] = prev.startAir
                transferInfo2['endAir'] = prev.endAir
                transferInfo2['startTime'] = str(prev.startTime)
                transferInfo2['endTime'] = str(prev.endTime)
                transferInfo2['flightName'] = prev.flightName
                transferInfo2['price'] = str(prev.price)
                transferInfo2['leftSeat'] = prev.leftSeat
                transferInfo2['state'] = prev.state
                transferInfo2['flightId'] = prev.id
                no.append(transferInfo2)
                count += 1
                data['root'].append(no)

        for third in thirds:
            if third.end == end:
                no = []
                transferInfo = {}
                transferInfo['order'] = 3
                transferInfo['start'] = third.start
                transferInfo['end'] = third.end
                transferInfo['startAir'] = third.startAir
                transferInfo['endAir'] = third.endAir
                transferInfo['startTime'] = str(third.startTime)
                transferInfo['endTime'] = str(third.endTime)
                transferInfo['flightName'] = third.flightName
                transferInfo['price'] = str(third.price)
                transferInfo['leftSeat'] = third.leftSeat
                transferInfo['state'] = third.state
                transferInfo['flightId'] = third.id
                no.append(transferInfo)
                prev = third.last
                transferInfo2 = {}
                transferInfo2['order'] = 2
                transferInfo2['start'] = prev.start
                transferInfo2['end'] = prev.end
                transferInfo2['startAir'] = prev.startAir
                transferInfo2['endAir'] = prev.endAir
                transferInfo2['startTime'] = str(prev.startTime)
                transferInfo2['endTime'] = str(prev.endTime)
                transferInfo2['flightName'] = prev.flightName
                transferInfo2['price'] = str(prev.price)
                transferInfo2['leftSeat'] = prev.leftSeat
                transferInfo2['state'] = prev.state
                transferInfo2['flightId'] = prev.id
                no.append(transferInfo2)
                prev = prev.last
                transferInfo3 = {}
                transferInfo3['start'] = prev.start
                transferInfo3['end'] = prev.end
                transferInfo3['order'] = 1
                transferInfo3['startAir'] = prev.startAir
                transferInfo3['endAir'] = prev.endAir
                transferInfo3['startTime'] = str(prev.startTime)
                transferInfo3['endTime'] = str(prev.endTime)
                transferInfo3['flightName'] = prev.flightName
                transferInfo3['price'] = str(prev.price)
                transferInfo3['leftSeat'] = prev.leftSeat
                transferInfo3['state'] = prev.state
                transferInfo3['flightId'] = prev.id
                no.append(transferInfo3)
                data['root'].append(no)
                count += 1

    return HttpResponse(json.dumps(data))


def getSeatlist(request):
    req = json.loads(request.body)
    flightId = req['flightId']
    flight = FlightInfo.objects.get(id=flightId)
    data = {
        'seat': flight.seatInfo
    }
    return HttpResponse(json.dumps(data))
